from sklearn.ensemble import VotingRegressor
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
from sklearn import svm
from sklearn import tree
from sklearn.metrics import mean_squared_error as MSE
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score

from sklearn.preprocessing import MinMaxScaler, StandardScaler
from sklearn.metrics import mean_squared_error
from sklearn.metrics import accuracy_score

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


# 导入数据
data = pd.read_csv(r"C:\Users\Lenovo\Desktop\Insightful & Vast USA Statistics\中位数填补_PCA降维后的数据.csv")

# 划分特征与标签
y = data.loc[:,data.columns == 'debt']
X = data.iloc[:,data.columns != 'debt']

# 划分训练集、测试集
Xtrain,Xtest,Ytrain,Ytest = train_test_split(X,y,test_size = 0.3,random_state = 420)


# 实例化各个模型
LinearRegression_model = LinearRegression()
Ridge_model = Ridge()
svm_model = svm.SVR()
tree_model = tree.DecisionTreeRegressor()

Voting_model = VotingRegressor([('LinearRegression_model', LinearRegression_model), ('Ridge_model',Ridge_model), ('svm_model',svm_model), ('tree_model',tree_model)])


# 训练模型
LinearRegression_model = LinearRegression_model.fit(Xtrain,Ytrain)
Ridge_model = Ridge_model.fit(Xtrain,Ytrain)
svm_model = svm_model.fit(Xtrain,Ytrain)
tree_model = tree_model.fit(Xtrain,Ytrain)

Voting_model = Voting_model.fit(Xtrain,Ytrain)


# 预测结果
yhat_LinearRegression_model = LinearRegression_model.predict(Xtest)
yhat_Ridge_model = Ridge_model.predict(Xtest)
yhat_svm_model = svm_model.predict(Xtest)
yhat_tree_model = tree_model.predict(Xtest)
yhat_Voting_model = Voting_model.predict(Xtest)

# 模型评估
print("LinearRegression 均方误差为：\n", MSE(yhat_LinearRegression_model,Ytest))
print("LinearRegression r^2分数:\n", LinearRegression_model.score(Xtest,Ytest))

print("-------------------------------------------")

print("Ridge_model 均方误差为：\n", MSE(yhat_Ridge_model,Ytest))
print("Ridge_model r^2分数:\n", Ridge_model.score(Xtest,Ytest))

print("-------------------------------------------")

print("svm_model 均方误差为：\n", MSE(yhat_svm_model,Ytest))
print("svm_model r^2分数:\n", svm_model.score(Xtest,Ytest))

print("-------------------------------------------")

print("tree_model_model 均方误差为：\n", MSE(yhat_tree_model,Ytest))
print("tree_model_model r^2分数:\n", tree_model.score(Xtest,Ytest))

print("-------------------------------------------")

print("Voting_model 均方误差为：\n", MSE(yhat_Voting_model,Ytest))
print("Voting_model r^2分数:\n", Voting_model.score(Xtest,Ytest))


